from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui

import sys

class QPainterTest(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(QPainterTest, self).__init__(parent)

        self.setWindowTitle("QPainterTest例子")

        self.resize(300, 400)

        self.text = "欢迎学习PyQt5"

    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)

        self.drawText(event, painter)
        self.drawLines(event, painter)
        painter.end()

    def drawText(self, event, qp):
        qp.setPen(QtGui.QColor(168, 34, 3))
        qp.setFont(QtGui.QFont('SimSun', 20))
        qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)

    def drawLines(self, event, qp):
        pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)

        # 画线
        # (20, 40) -----> (250, 40)
        qp.drawLine(20, 40, 250, 40)

        pen.setStyle(QtCore.Qt.DashLine)
        qp.setPen(pen)
        qp.drawLine(20, 80, 250, 80)

        pen.setStyle(QtCore.Qt.DashDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 120, 250, 120)

        pen.setStyle(QtCore.Qt.DotLine)
        qp.setPen(pen)
        qp.drawLine(20, 160, 250, 160)

        pen.setStyle(QtCore.Qt.CustomDashLine)

        #-  -----    -  -----    
        #一格线 两格空格 五格线 四格空格
        pen.setDashPattern([1,2,5,4])
        qp.setPen(pen)
        qp.drawLine(20, 240, 250, 240)
        
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    QtWidgets.QApplication.setStyle("Fusion")
    win = QPainterTest()
    win.show()
    sys.exit(app.exec_())